package com.kx.kxblog.service.impl;

import com.kx.kxblog.dao.SiteDao;
import com.kx.kxblog.entity.Site;
import com.kx.kxblog.enums.ResultCode;
import com.kx.kxblog.exception.ResultException;
import com.kx.kxblog.service.SiteService;
import com.kx.kxblog.util.SystemContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SiteServiceImpl extends AbstractServiceImpl<Site, Integer, SiteDao> implements SiteService {
    @Autowired
    public void setDao(SiteDao dao){
        this.dao = dao;
    }

    @Override
    public Site my() {
        return dao.findByUserId(SystemContext.getLoginUser().getId());
    }

    @Override
    public Site save(Site entity) {
        Site sourceSite = dao.findByUserId(SystemContext.getLoginUser().getId());
        if(entity.getId()==null) entity.setId(sourceSite.getId());
        else if(!sourceSite.getId().equals(entity.getId())) throw new ResultException(ResultCode.NO_PERMISSION);
        return super.save(entity);
    }
}
